from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger

from .forms import EmailPostForm, CommentForm
from django.core.mail import send_mail
from .models import Post
from taggit.models import Tag 
from django.db.models import Count 


def post_list(request,tag_slug=None):
	object_list = Post.objects.filter(status="published")
	tag = None #新增一个属性，默认值是None
	if tag_slug:
		tag = get_object_or_404(Tag,slug=tag_slug)   #这是根据tag_slug的值进行标签的查询结果
		object_list = object_list.filter(tags__in=[tag])   #根据查询到的标签，返回文章对象列表

	paginator = Paginator(object_list,1) #每页呈现1条
	page = request.GET.get('page')       #如果以get方式请求，则获得当前页的页码

	try:
		current_page = paginator.page(page)
		posts = current_page.object_list
	except PageNotAnInteger:
		current_page = paginator.page(1)
		posts = current_page.object_list
	except EmptyPage:
		current_page = paginator(paginator.num_pages)
		posts = current_page.object_list


	return render(request,'blog/post/list.html',{'posts':posts,'page':current_page,'tag':tag})

# def post_detail(request,post_id):
# 	post = get_object_or_404(Post,id=post_id)
# 	return render(request,'blog/post/detail.html',{"post":post,"post_id":post_id})



def post_detail(request, year, month, day, post):    #以前的参数是`post_id`，这里修改为现状
    #post = get_object_or_404(Post, id=post_id)      #原来是通过`id`从数据库中读取相应的文章，现在不用这种方式，把这句话注释或者删除，改为用下面的方式。
    post = get_object_or_404(Post, slug=post, status="published", published__year=year, published__month=month, published__day=day)
    comments = post.comments.filter(active=True)

    if request.method == 'GET':
    	comment_form = CommentForm()

    if request.method == 'POST':
    	comment_form = CommentForm(data=request.POST)
    	if comment_form.is_valid():
    		new_comment = comment_form.save(commit=False)
    		new_comment.post = post 
    		new_comment.save()
    post_tags_ids = post.tags.values_list('id',flat=True)
    similar_posts = Post.objects.filter(tags__in=post_tags_ids).exclude(id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-published')[:4]

    return render(request, 'blog/post/detail.html', {"post":post,"comments":comments,"comment_form":comment_form,"similar_posts":similar_posts
    })    #因为没有`post_id`，所以也不向模板传那个值了。

def post_share(request,post_id):
	post = get_object_or_404(Post,id=post_id,status="published")
	sent = False

	if request.method == "GET":
		form = EmailPostForm()

	if request.method == "POST":
		form = EmailPostForm(request.POST)
		if form.is_valid():
			cd = form.cleaned_data
			post_url = request.build_absolute_uri(post.get_absolute_url())
			subject = "{0}({1}) 推荐你阅读 '{2}'".format(cd['name'],cd['email'],post.title)
			message = "文章 '{0}' 网址 {1}\n\n{2} 的评论:{3}".format(post.title,post_url,cd['name'],cd['comments'])
			send_mail(subject,message,"515017605@qq.com",[cd['to']])
			sent = True 

	return render(request,'blog/post/share.html',{'post':post,"form":form,'sent':sent},)
